<template>
  <div>
    <div class="flex-end">
      <el-button type="primary" size="small" @click="visible = true"
        >添加培训经历</el-button
      >
    </div>
    <el-table :data="tableData" border class="table-base">
      <el-table-column prop="start" label="开始日期"></el-table-column>
      <el-table-column prop="end" label="结束日期"></el-table-column>
      <el-table-column prop="hour" label="培训小时"> </el-table-column>
      <el-table-column prop="address" label="培训地点"> </el-table-column>
      <el-table-column prop="organizers" label="举办单位"> </el-table-column>
      <el-table-column prop="content" label="培训内容"> </el-table-column>
      <el-table-column prop="creation" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="handleEdit(scope.$index)"
            type="text"
            size="small"
            >编辑</el-button
          >
          <el-button
            @click.native.prevent="handleDeletePre(scope.$index)"
            type="text"
            class="btn-delete"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加培训经历" :visible.sync="visible">
      <el-form label-width="100px" :model="form" ref="trainingForm">
        <el-form-item label="开始日期:" prop="start">
          <el-date-picker
            v-model="form.start"
            style="width: 195px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期:" prop="end">
          <el-date-picker
            v-model="form.end"
            style="width: 195px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="培训地点:" prop="address">
          <el-input
            v-model="form.address"
            placeholder="请输入培训地点"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="举办单位:" prop="organizers">
          <el-input
            v-model="form.organizers"
            placeholder="请输入举办单位"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="培训小时:" prop="hour">
          <el-input
            v-model="form.hour"
            placeholder="请输入培训小时"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="培训内容:" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入培训内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="handleAdd"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="编辑培训经历" :visible.sync="editVisible">
      <el-form label-width="100px" :model="editForm" ref="editForm">
        <el-form-item label="开始日期:" prop="start">
          <el-date-picker
            v-model="editForm.start"
            style="width: 195px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期:" prop="end">
          <el-date-picker
            v-model="editForm.end"
            style="width: 195px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="培训地点:" prop="address">
          <el-input
            v-model="editForm.address"
            placeholder="请输入培训地点"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="举办单位:" prop="organizers">
          <el-input
            v-model="editForm.organizers"
            placeholder="请输入举办单位"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="培训小时:" prop="hour">
          <el-input
            v-model="editForm.hour"
            placeholder="请输入培训小时"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="培训内容:" prop="content">
          <el-input
            v-model="editForm.content"
            type="textarea"
            placeholder="请输入培训内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveEdit"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCurrentTime } from "@/utils/qualificationData";
export default {
  name: "hr-training",
  data() {
    return {
      tableData: [],
      visible: false,
      editVisible: false,
      editForm: {},
      editNum: undefined,
      form: {},
    };
  },
  created() {},
  watch: {
    tableData: {
      handler() {
        this.$emit("transimitTrainingData", this.tableData);
      },
      deep: true,
    },
  },
  methods: {
    handleAdd() {
      this.$refs["trainingForm"].validate((valid) => {
        if (valid) {
          let obj = {
            ...this.form,
            ...{ creation: getCurrentTime() },
          };
          this.tableData.push(obj);
          this.visible = false;
          this.$refs["trainingForm"].resetFields();
          this.form = {};
        }
      });
    },
    handleEdit(index) {
      this.editForm = this.tableData[index];
      this.editVisible = true;
      this.editNum = index;
    },
    handleSaveEdit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          let obj = JSON.parse(JSON.stringify(this.editForm));
          obj.creation = getCurrentTime();
          this.$set(this.tableData, this.editNum, obj);
          this.editVisible = false;
          this.$refs["editForm"].resetFields();
          this.editNum = undefined;
        }
      });
    },
    handleDeletePre(index) {
      this.$confirm("确定要删除此段培训经历吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="less" scoped>
</style>
